﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability;

namespace EntLibContrib.Resource.Configuration.Manageability
{
	/// <summary>
	/// Represents a mapper for <see cref="CustomResourceManagerSetting"/> configuration to Wmi.
	/// </summary>
	internal static class CustomResourceManagerDataWmiMapper
	{
		#region Public Static Methods
		/// <summary>
		/// Creates the <see cref="CustomResourceManagerSetting"/> instances that describe the
		/// configurationObject.
		/// </summary>
		/// <param name="data">The configuration object for instances that must be managed.</param>
		/// <param name="wmiSettings">A collection to where the generated WMI objects are to be added.</param>
		public static void GenerateWmiObjects(CustomResourceManagerData data, ICollection<ConfigurationSetting> wmiSettings)
		{
			wmiSettings.Add(new CustomResourceManagerSetting(data.Name,
				data.Type.AssemblyQualifiedName,
				CustomDataWmiMapperHelper.GenerateAttributesArray(data.Attributes)));
		}
		#endregion

		#region Internal Static Methods
		/// <summary>
		/// Registers the WMI types.
		/// </summary>
		internal static void RegisterWmiTypes()
		{
			try
			{
				ManagementEntityTypesRegistrar.SafelyRegisterTypes(typeof(CustomResourceManagerSetting));
			}
			catch (ArgumentException ex)
			{
				// ignore error that states registry entry already exists
				if (Debugger.IsAttached)
					Debug.WriteLine(ex.Message);
			}
		}
		#endregion
	}
}
